package edu.miami.group.photolog;

import java.util.HashMap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
//==========================================================================================
public class MainActivity extends Activity implements
SimpleCursorAdapter.ViewBinder, AdapterView.OnItemLongClickListener,
AdapterView.OnItemClickListener {
	private PhlogBase database;
	private Cursor cursorFill;
	private long itemSelected;
	private final int THE_DIALOG = 1;
	private final int THE_DIALOG_HELP = 2;
	private AlertDialog theDialog;
	private static final int ACTIVITY_SHOW_VIEW = 1;
	private static final int ACTIVITY_CREATE = 2;
//------------------------------------------------------------------------------------------
//onCreate
	protected void onCreate(Bundle savedInstanceState) {

		String[] queryFields = { "picVid", "title" };

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		String tableName = getIntent().getStringExtra(Login.TABLE_NAME);
		//create the database
		database = new PhlogBase(this, tableName);

		// Fetch all the data from the database and fill the ListView
		cursorFill = database.fetchAllPosts();
		ListView theList = (ListView) findViewById(R.id.list);
		// Need to understand how to query fields to get the listView running
		//Create the adapter to populate the listView
		SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(this,
				R.layout.little_picture_names_list_item, cursorFill,
				queryFields, new int[] { R.id.item_picture,
				R.id.item_description });
		//set the ViewBinder
		myAdapter.setViewBinder(this);
		theList.setOnItemLongClickListener(this);
		theList.setOnItemClickListener(this);
		theList.setAdapter(myAdapter);
	}
//--------------------------------------------------------------------------------------
//sets the onlClick of the List view to open up the View activity
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long rowId) {
		itemSelected = rowId;
		ContentValues returnObject = database.getDataByID(rowId);
		Intent viewIntent = new Intent();
		Log.i("setting the classname", "seting the class name");
		viewIntent.setClassName("edu.miami.group.photolog",
				"edu.miami.group.photolog.ViewPhlog");
		viewIntent.putExtra("databaseObject", returnObject);
		viewIntent.putExtra(Login.TABLE_NAME, getIntent().getStringExtra(Login.TABLE_NAME));
		Log.i("starting the next activity", "starting the next activity");
		startActivityForResult(viewIntent, ACTIVITY_SHOW_VIEW);
	}
//-------------------------------------------------------------------------------------------
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
//---------------------------------------------------------------------------------------------
//creates the two dialogs for the two seperate cases
	protected Dialog onCreateDialog(int dialogId) {
		AlertDialog.Builder dialogBuilder;
		LayoutInflater dialogInflator;

		dialogBuilder = new AlertDialog.Builder(this);
		switch (dialogId) {
		case THE_DIALOG:
			dialogInflator = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			View dialogView = dialogInflator.inflate(
					R.layout.little_picture_names_dialog_layout,
					(ViewGroup) findViewById(R.id.dialog_root));
			dialogBuilder.setView(dialogView);
			break;
		case THE_DIALOG_HELP:
			dialogInflator = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			View dialogViewHelp = dialogInflator.inflate(
					R.layout.help_dialog_layout,
					(ViewGroup) findViewById(R.id.dialog_root));
			dialogBuilder.setView(dialogViewHelp);
		default:
			break;
		}

		theDialog = dialogBuilder.create();
		// theDialog.setOnDismissListener(this);
		return theDialog;

	}

	// -----------------------------------------------------------------------------
	// prepares the dialog
	protected void onPrepareDialog(int dialogId, Dialog dialog) {

	}

	// dismiss button click handler
	public void myClickListener(View view) {
		switch (view.getId()) {
		//Deletes an item from the database upon opening the dialog
		case R.id.delete:
			//gets the itemSelected 
			ContentValues object = database.getDataByID(itemSelected);
			database.deletePost(object.getAsInteger("_id").intValue());
			cursorFill.requery();
			dismissDialog(THE_DIALOG);
			break;
		//cancels the dialog
		case R.id.cancel:
			theDialog.dismiss();
			break;
		//goes the the create activity
		case R.id.create:
			Intent createIntent = new Intent();
			//starts the new intent
			createIntent.setClassName("edu.miami.group.photolog",
					"edu.miami.group.photolog.CreatePhlog");
			createIntent.putExtra(Login.TABLE_NAME, getIntent().getStringExtra(Login.TABLE_NAME));
			startActivityForResult(createIntent, ACTIVITY_CREATE);
		}

	}
//-----------------------------------------------------------------------------------
	public boolean onItemLongClick(AdapterView<?> arg0, View view, int arg2,
			long id) {
		itemSelected = id;
		showDialog(THE_DIALOG);
		return true;
	}
//---------------------------------------------------------------------------------------
//Sets the view of the list view
	public boolean setViewValue(View view, Cursor cursor, int columnIndex) {

		if (columnIndex == cursor.getColumnIndex("picVid")) {
			//sets the thumbnail of the image
			byte[] picture = cursor.getBlob(cursor.getColumnIndex("picVid"));
			Bitmap bmp = BitmapFactory.decodeByteArray(picture, 0,
					picture.length);
			((ImageView) view).setImageBitmap(bmp);
			return true;
		}

		return false;
	}
//----------------------------------------------------------------------------------------
//On Activity Result
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.i("RIGHT BEFORE SWITCH", "IN ON ACTIVITY RESULT");
		switch (requestCode) {

		// case for showing the view
		case ACTIVITY_SHOW_VIEW:
			switch (resultCode) {
			case RESULT_OK:
				cursorFill.requery();
				break;

			case RESULT_CANCELED:
				break;

			}
		//case for the create 
		case ACTIVITY_CREATE:
			switch (resultCode) {
			case RESULT_OK:
				Log.i("RIGHT BEFORE QUERY", "IN CREATE");
				cursorFill.requery();
				break;

			case RESULT_CANCELED:
				break;
			}
		}
		//requeries for to update the listView
		cursorFill.requery();
	}

	// -----------------------------------------------------------------------------
	//onOptionsItemSelected Menu is for menu options
	public boolean onOptionsItemSelected(MenuItem item) {
		String[] queryFields = { "picVid", "title" };
		ListView theList = (ListView) findViewById(R.id.list);
		SimpleCursorAdapter myAdapter;
		switch (item.getItemId()) {
		//pulls up the Help Dialog
		case R.id.Help:
			
			showDialog(THE_DIALOG_HELP);
			return true;
		//sorts by title
		case R.id.Title:
			//fetches all the post according by titles 
			cursorFill = database.fetchAllPosts("title");
			Log.i("Sort by title", " Sort by title");
			//sets a new adapter to sort by the title
			myAdapter = new SimpleCursorAdapter(this,
					R.layout.little_picture_names_list_item, cursorFill,
					queryFields, new int[] { R.id.item_picture,
					R.id.item_description });
			myAdapter.setViewBinder(this);
			theList.setAdapter(myAdapter);
			Log.i("Sort by title", " Sort by title");
			cursorFill.requery();
			return true;

		case R.id.Date:
			//fetches all the post according to the date
			cursorFill = database.fetchAllPosts("date");
			//sets a new adapter to sort by date
			myAdapter = new SimpleCursorAdapter(this,
					R.layout.little_picture_names_list_item, cursorFill,
					queryFields, new int[] { R.id.item_picture,
					R.id.item_description });
			myAdapter.setViewBinder(this);
			theList.setAdapter(myAdapter);
			cursorFill.requery();
			return true;

		}

		return false;

	}

}
